package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.Result;
import com.atguigu.yygh.common.config.YyghException;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author zhmwi
 * @Date 2021/7/8
 * @Dersciption
 */
@RestController
@Api("医院管理Api接口")
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //接收request请求中参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将参数转换为Map
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //医院code校验
        String hoscode = (String)paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)){
            throw new YyghException(2001,"失败");
        }
        //签名校验
        //1.获取从医院系统传递过来的签名，签名已经进行MD5加密
        String hospitalSign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编码查询数据库 查询本地的签名秘钥
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询到的签名进行MD5加密
        if(!MD5.encrypt(signKey).equals(hospitalSign)){
            throw new YyghException(20001,"MD5校验失败");
        }
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);
        hospitalService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation("获取医院信息")
    @PostMapping("hospital/show")
    public Result showHospital(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)){
            throw new YyghException(20001,"获取医院信息时，传递医院编号时为空！");
        }
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation("上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //参数验空
        //参数校验
        departmentService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation("获取分页科室列表")
    @PostMapping("department/list")
    public Result showDepartment(HttpServletRequest request){
        //转化参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");

        //获取当前页
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        //获取每页显示几条数据
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        //验证签名

        //封装查询对象
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.queryByPage(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    @ApiOperation("上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        scheduleService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation("分页条件查询排班")
    @PostMapping("schedule/list")
    public Result showSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page"))
                ? 1 : Integer.parseInt((String)(paramMap.get("page")));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))
                ? 10 : Integer.parseInt((String)(paramMap.get("limit")));
        //封装查询条件
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        Page<Schedule> pageMoule = scheduleService.selectPage(page,limit,scheduleQueryVo);
        return Result.ok(pageMoule);
    }

    @ApiOperation("删除分页")
    @PostMapping("schedule/remove")
    public Result removeSchdule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
}
